package com.yaxin.common.utils;

import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * @author lgj
 * @version 1.0
 * @description 保证事务成功后再发消息
 * @date 2025/7/20 9:05
 */
public class TransactionUtils {

    public static void doAfterCommit(Runnable runnable) {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
                @Override
                public void afterCommit() {
                    runnable.run();
                }
            });
        } else {
            runnable.run(); // 没有事务时直接执行
        }
    }
}
